<?php
    //valida que venga los datos
    if(isset($_GET['nueva_marca']) && isset($_GET['ACCION'])){
        $nueva_marca = $_GET['nueva_marca'];
        $accion = $_GET['ACCION'];
    }else{
        $response = array("RESULT" => "1","ERROR" =>"Error");
        echo json_encode($response);
        exit();   
    }
    
    
    
    require_once('../php/class/marcas.class.php');
    $oMarca = new marca();
    
    switch($accion){
        case "ADD":
			$orden = $_GET['orden'];
			
			if($oMarca->unique("DESCRIPCION","'".$nueva_marca."'")){
				$response = array("RESULT" => "1","ERROR" =>"Ya existe una marca con esa descripci&oacute;n");
                echo json_encode($response);
                exit();
				}
				
            $oMarca->ID = $oMarca->getlastid();
            $oMarca->DESCRIPCION = $nueva_marca;
            $oMarca->ORDEN = $orden;
            $oMarca->ID_STATUS = 1; //activo
            
            if(!@$oMarca->add()){
                $response = array("RESULT" => "1","ERROR" =>"Error guardando marca");
                echo json_encode($response);
                exit();   
            }else{
                $response = array("RESULT" => "0","ERROR" =>"none");
                echo json_encode($response);
                exit(); 
            }
        break;
        
        case "MODIFY":
            $id_marca = $_GET['id_marca'];
            $orden = $_GET['orden'];
            $oMarca->search($id_marca);
            $nueva_marca = $_GET['nueva_marca'];
            
            if($oMarca->DESCRIPCION != $nueva_marca){
				if($oMarca->unique("DESCRIPCION","'".$nueva_marca."'")){
					$response = array("RESULT" => "1","ERROR" =>"Ya existe una marca con esa descripci&oacute;n");
					echo json_encode($response);
					exit();
				}	
			}
            
            
            $oMarca->DESCRIPCION = $nueva_marca;
            $oMarca->ORDEN = $orden;
            
                if($oMarca->modify()){
                    $response = array("RESULT" => "0","ERROR" =>"none");
                    echo json_encode($response);
                    exit();     
                }else{
                    $response = array("RESULT" => "1","ERROR" =>"Error modificando marca");
                    echo json_encode($response);
                    exit();
                }
        break;
        
        case "DELETE":
            $id_marca = $_GET['id_marca'];
            $oMarca->search($id_marca);
            $oMarca->ID_STATUS = 0; //inactivo
                if($oMarca->modify()){
                    $response = array("RESULT" => "0","ERROR" =>"none");
                    echo json_encode($response);
                    exit();     
                }else{
                    $response = array("RESULT" => "1","ERROR" =>"Error borrando marca");
                    echo json_encode($response);
                    exit();
                }
        break;
            
    }
     
?>
